﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Group3.MediaTool.Data.Generator;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Group3.MediaTool.Interface;
using System.IO;

namespace Group3.MediaTool.Data.DefinitionBuilder
{
    /// <summary>
    /// Interaktionslogik für DataDefinitionBuilder.xaml
    /// </summary>
    public partial class DataDefinitionBuilder : UserControl
    {
        String _currentClass = "";
        DataDefinitionClass _defClass;
        List<String> _lines = new List<String>();
        List<String> _classes = new List<string>();


        public String ReturnValue
        {
            get;
            set;
        }

        public DataDefinitionBuilder(DataDefinitionClass pDefClass)
        {
            _defClass = pDefClass;
            InitializeComponent();
            createAttributeDatatypes();
            createPage();
        }

        void createPage()
        {

            //foreach (KeyValuePair<IObjectDefinition, IDataGenerator> itm in _defClass.DataPairs)
            {
                //_classes.Add(itm.Key.OrmName);
            }
            listOverview.ItemsSource = _classes;
            
        }

        public void OverviewItemClick(object sender, System.Windows.RoutedEventArgs e)
        {
            _currentClass = "";
            ListViewItem item = e.Source as ListViewItem;
            String line = item.DataContext as String;

            //foreach (KeyValuePair<IObjectDefinition, IDataGenerator> itm in _defClass.DataPairs)
            {
                //if (line == itm.Key.OrmName)
                {
                    _currentClass = line;
                //    _lines = itm.Value.DefinitionLines;
                }
            }
            listDetail.ItemsSource = _lines;
        }

        private void AddClassButton_Click(object sender, RoutedEventArgs e)
        {
            //Create new class
        }

        private void AddLineButton_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(tAttributeName.Text))
            {
                MessageBox.Show("No attribute name", "Error", MessageBoxButton.OK);
            }
            else
            {
                if (ifAttrExists(tAttributeName.Text))
                {
                    MessageBox.Show("Attribute exists", "Error", MessageBoxButton.OK);
                }else{
                    //DataLine dl = new DataLine("a", _currentClass, tAttributeName.Text, ((String)((ComboBoxItem)cbAttributeDatatype.SelectedItem).Tag), ((String)((ComboBoxItem)cbAttributeType.SelectedItem).Tag));
                    //_lines.Add(dl);
                    listDetail.Items.Refresh();
                }
            }
            //add to lines
        }

        private bool ifAttrExists(string p)
        {
            //foreach (DataLine dl in _lines)
            {
                //if (dl.AttributeName == p)
                {
                    return true;
                }
            }
            //return false;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            //_defClass.writeDefinitionToFile();
            //_defClass.readDefinitionFromFile();
            listOverview.Items.Refresh();
            //save lines to file
        }

        private void ReferenceItem_Selected(object sender, RoutedEventArgs e)
        {
            
            uClassSelect  cs= new uClassSelect(_classes, this);
            cs.ShowDialog();
            
            RefItem.Tag = "ref(" + ReturnValue.ToLower()  + ")";
            RefItem.Content = "Reference " + ReturnValue;
            ReturnValue = "";
            cbAttributeType.SelectedItem = classRef;
        }

        void createAttributeDatatypes()
        {
            String path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources/datatypes.txt");

            // create a reader and open the file
            String strLine = "";
            TextReader sr = new StreamReader(path);
            while ((strLine = sr.ReadLine()) != null)
            {
                ComboBoxItem cb = new ComboBoxItem();
                cb.Content = strLine;
                cb.Tag = strLine;
                cbAttributeDatatype.Items.Add(cb);
            }
            sr.Close();
        }

        private void nvarcharItem_Selected(object sender, RoutedEventArgs e)
        {
            ComboBoxItem itm = (ComboBoxItem)sender;
            String value = (String)itm.Content;
            uValueSelect cs = new uValueSelect(this);
            cs.ShowDialog();

            if (!String.IsNullOrEmpty(ReturnValue))
            {
                itm.Tag = value + "(" + ReturnValue + ")";
                itm.Content = value + "(" + ReturnValue + ")";
                ReturnValue = "";
                cbAttributeType.SelectedItem = normalAttr;
            }
        }

    }

}
